﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using DAO;

namespace GUI
{
    public partial class CuonSach : UserControl
    {
        
        public CuonSach()
        {
            InitializeComponent();            
           
        }        
        //Ham ho tro-----------------------------------------------

        private StringBuilder ma_sach= new StringBuilder();
        CuonSachDTO cuonSach = new CuonSachDTO();
        private void LoadData()
        {
            dataGridViewCuonSach.DataSource = CuonSachBUS.TimTatCaCuonSach();
            this.cmbox_LoaiSach.DataSource = LoaiSachBUS.TimTatCaLoaiSach();
            this.cmbox_LoaiSach.DisplayMember = "Tên Loại Sách";
            this.cmbox_LoaiSach.ValueMember = "Mã Loại Sách";
            this.cmbox_TuaDe.DataSource = TuaDeSachBUS.TimTuaDeSachBangLoaiSach(this.cmbox_LoaiSach.SelectedValue.ToString());
            this.cmbox_TuaDe.DisplayMember = "TenTuaDe";
            this.cmbox_TuaDe.ValueMember = "MaTuaDe";
            cbLanXB.DataSource = LanXBDAO.TimTatCaLanXB();
            cbLanXB.DisplayMember = "Mã Lần XB";
            cbLanXB.ValueMember = "Mã Lần XB";
         
            try
            {
                ma_sach.Append(cmbox_TuaDe.SelectedValue.ToString());
                ma_sach.Append(Int32.Parse(tbSTT.Text).ToString().PadLeft(2,'0'));
            }
            catch (Exception ex)
            {
                               
            }
        }
        public delegate void GetString(String str);

        public GetString MyGetData;

        private String getMaSach()
        {
            try
            {
                ma_sach.Remove(0, ma_sach.Length);
                ma_sach.Append(cmbox_TuaDe.SelectedValue.ToString());
                ma_sach.Append(Int32.Parse(tbSTT.Text).ToString().PadLeft(2, '0'));
                
            }
            catch (Exception)
            {                                
            }
            return ma_sach.ToString();
        }

        private void FastNavigation(String str)//mat di vi text STT change
        {
            try
            {
                for (int i = 0; i < dataGridViewCuonSach.Rows.Count; i++)
                {
                    if (dataGridViewCuonSach.Rows[i].Selected)
                    {
                        dataGridViewCuonSach.Rows[i].Selected = false;
                    }
                }
                for (int i = 0; i < dataGridViewCuonSach.Rows.Count; i++)
                {
                    if (dataGridViewCuonSach.Rows[i].Cells[0].Value.ToString().Equals(str))
                    {
                        dataGridViewCuonSach.Rows[i].Selected = true;
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        private bool UploadData()
        {      
            if (txtBox_MaSach.Text == "")
            {
                txtBox_MaSach.Text = getMaSach();
            }
            if (txtBox_MaSach.Text == "")
            {
                return false;
            }
            cuonSach.MaSach = txtBox_MaSach.Text;
            cuonSach.Lent = cmbox_Lent.SelectedItem.ToString();
            cuonSach.MaLanXB = byte.Parse(cbLanXB.Text);
            cuonSach.MaTuaDe = cmbox_TuaDe.SelectedValue.ToString();
            cuonSach.NgayNhap = DateTime.Parse(dateTime_NgayNhap.Value.ToString());
            cuonSach.SoThuTu = Int32.Parse(tbSTT.Text);
            cuonSach.TinhTrang = txtBox_TinhTrang.Text;
            return true;
        }
        private bool CheckDaDien()
        {
            if (txt_MaTuaDe.Text =="" || tbSTT.Text==""|| cbLanXB.Text==""|| txtBox_TinhTrang.Text=="" || cmbox_Lent.Text==""|| cmbox_LoaiSach.Text==""|| cmbox_TuaDe.Text=="")
            {
                return false;
            }
            return true;
        }

        //Ham thiet ke------------------------------------------------------
        private void CuonSach_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void cmbox_LoaiSach_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.cmbox_TuaDe.DataSource =
                TuaDeSachBUS.TimTuaDeSachBangLoaiSach(this.cmbox_LoaiSach.SelectedValue.ToString());
                this.cmbox_TuaDe.DisplayMember = "TenTuaDe";
                this.cmbox_TuaDe.ValueMember = "MaTuaDe";
                txtBox_MaSach.Text = getMaSach();
            }
            catch(Exception ex){}
                     
        }

        private void cmbox_TuaDe_SelectedIndexChanged(object sender, EventArgs e)
        {
            txt_MaTuaDe.Text = cmbox_TuaDe.SelectedValue.ToString();
            txtBox_MaSach.Text = getMaSach();
        }               
        private void dataGridViewCuonSach_SelectionChanged(object sender, EventArgs e)
        {
            if (this.dataGridViewCuonSach.CurrentRow != null)
            {
                try
                {
                    this.txtBox_MaSach.Text = dataGridViewCuonSach.SelectedRows[0].Cells[0].Value.ToString();
                    this.txt_MaTuaDe.Text = dataGridViewCuonSach.SelectedRows[0].Cells[1].Value.ToString();
                    this.tbSTT.Text = dataGridViewCuonSach.SelectedRows[0].Cells[4].Value.ToString();
                    this.cbLanXB.Text = dataGridViewCuonSach.SelectedRows[0].Cells[3].Value.ToString();
                    this.txtBox_TinhTrang.Text = dataGridViewCuonSach.SelectedRows[0].Cells[5].Value.ToString();
                    this.dateTime_NgayNhap.Value = DateTime.Parse(dataGridViewCuonSach.SelectedRows[0].Cells[6].Value.ToString());
                    this.cmbox_LoaiSach.SelectedValue = txt_MaTuaDe.Text.Substring(0,4);
                    this.cmbox_TuaDe.SelectedValue = txt_MaTuaDe.Text;

                    //this.dateTime_NgayNhap.Value = DateTime.Parse(dataGridViewCuonSach.SelectedRows[0].Cells[5].Value.ToString());
                    if (dataGridViewCuonSach.SelectedRows[0].Cells[7].Value.ToString() == "YES")
                    {
                        this.cmbox_Lent.SelectedItem = cbIYes;
                    }
                    else this.cmbox_Lent.SelectedItem = cbINo;
                }
                catch (System.Exception ex)
                {

                }
            }
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            if (CheckDaDien())
            {

                if (!UploadData())
                {
                    textBox_ThongbaoSach.Text = "Thêm thất bại! Dữ liệu nhập chưa đủ hoặc chưa đúng!";
                    return;
                }
                if (CuonSachDAO.CheckCuonSachBangID(txtBox_MaSach.Text))
                {
                    textBox_ThongbaoSach.Text = "Mã sách đã tồn tại.";
                }
                else
                {
                    if (CuonSachBUS.ThemCuonSach(cuonSach))
                    {
                        textBox_ThongbaoSach.Text = "Thêm thành công.";
                    }
                    dataGridViewCuonSach.DataSource = CuonSachBUS.TimTatCaCuonSach();
                }
            }

            else
            {
                textBox_ThongbaoSach.Text = "Bạn chưa điền đầy đủ thông tin.";
            }
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {           
            if (CuonSachDAO.CheckCuonSachBangID(txtBox_MaSach.Text))
            {                
                if (CuonSachBUS.XoaCuonSach(txtBox_MaSach.Text))
                {
                    
                    textBox_ThongbaoSach.Text = "Bạn đã xóa sách thành công.";
                    dataGridViewCuonSach.DataSource = CuonSachBUS.TimTatCaCuonSach();
                }
            }
            else
            {
                textBox_ThongbaoSach.Text = "Không tìm thấy cuốn sách bạn muốn xóa.";
            }
            
        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            if (CheckDaDien())
            {
                UploadData();

                if (CuonSachBUS.SuaCuonSach(cuonSach))
                {
                    textBox_ThongbaoSach.Text = "Bạn đã sửa thành công.";
                    dataGridViewCuonSach.DataSource = CuonSachBUS.TimTatCaCuonSach();
                }
                else
                {
                    textBox_ThongbaoSach.Text = "Sửa thất bại.";
                }
            }
            else
            {
                textBox_ThongbaoSach.Text = "Bạn chưa điền đầy đủ thông tin.";
            }
        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            LoadData();
            txt_MaTuaDe.Text = ""; tbSTT.Text = ""; cbLanXB.Text = ""; txtBox_MaSach.Text = "";
            txtBox_TinhTrang.Text = ""; cmbox_Lent.Text = ""; cmbox_LoaiSach.Text = ""; cmbox_TuaDe.Text = "";
        }       

        private void textBox_ThongbaoSach_TextChanged(object sender, EventArgs e)
        {
            if (MyGetData!=null)
            {
                MyGetData(textBox_ThongbaoSach.Text);
            }
        }

      

        private void chbox_EditKey_CheckedChanged(object sender, EventArgs e)
        {
            if (txt_MaTuaDe.ReadOnly == false && txtBox_MaSach.ReadOnly == false)
            {
                txt_MaTuaDe.ReadOnly = true;
                txtBox_MaSach.ReadOnly = true;
            }
            else
            {
                txt_MaTuaDe.ReadOnly = false;
                txtBox_MaSach.ReadOnly = false;
            }
        }

       

        private void txtBox_LanXB_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+") == true || e.KeyChar == '\b')
                e.Handled = false;
            else
            {
                e.Handled = true;
            }
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }
       

        private void tbSTT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+") == true || e.KeyChar == '\b')
                e.Handled = false;
            else
            {
                e.Handled = true;
            }
        }

        private void cbLanXB_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+") == true || e.KeyChar == '\b')
                e.Handled = false;
            else
            {
                e.Handled = true;
            }
        }

        private void tbSTT_TextChanged(object sender, EventArgs e)
        {
            txtBox_MaSach.Text = getMaSach();

        }
        
    }
}
